package com.qf.service.impl;

import com.qf.pojo.Man;
import com.qf.service.WelManService;
import com.qf.utils.JedisPoolUtils;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Tuple;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class WelManServiceImpl implements WelManService {
    @Override
    public List<Man> getWelMans() {

        Jedis jedis = JedisPoolUtils.getJedis();
        //从大到小查询前10名
        Set<Tuple> wel_man = jedis.zrevrangeWithScores("wel_man", 0, 9);

        List<Man> manList = new ArrayList<>();
        for (Tuple tuple : wel_man) {
            //获取name
            String name = tuple.getElement();
            //获取分数
            double score = tuple.getScore();

            manList.add(new Man(name,score));
        }

        jedis.close();
        return manList;
    }

    @Override
    public void incr(String name, double score) {
        Jedis jedis = JedisPoolUtils.getJedis();
        jedis.zadd("wel_man",score,name);

        jedis.close();
    }
}
